<template>
  <div class="contract">
    <div class="common">
      <div class="common-input">
        <el-input
          v-model="commonValue"
          placeholder="用户昵称"
          clearable
        ></el-input>
      </div>

      <div class="common-input">
        <el-select v-model="commonUpdateValue" placeholder="请选择" clearable>
          <el-option
            v-for="item in commonUpdateOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>

      <div class="common-input">
        <el-date-picker
          v-model="commonDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
           format="yyyy-MM-dd" value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div
        class="common-button"
        @click="replacementHit"
        style="background: #999"
      >
        重置
      </div>
      <div
        class="common-button"
        @click="uploadingHit(0)"
        style="background: #2a82e4"
      >
        上传合同pdf
      </div>
    </div>

    <div class="table" v-loading="loading">
      <el-table
        tooltip-effect="dark"
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{
          color: '#333333',
          backgroundColor: '#f3f3f3',
        }"
      >
        <el-table-column type="selection" width="70" align="center">
        </el-table-column>
        <el-table-column prop="userId" label="ID"> </el-table-column>
        <el-table-column prop="shopUserName" label="申请人"> </el-table-column>
        <el-table-column prop="shopContractCreateTime" label="申请日期">
        </el-table-column>
        <el-table-column prop="lastShopUserName" label="上级">
        </el-table-column>
        <el-table-column prop="shopContractBreak" label="备注" width="400">
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.shopContractStatus == 0">待审批</el-tag>
            <el-tag v-if="scope.row.shopContractStatus == 1" type="success"
              >已通过</el-tag
            >
            <el-tag v-if="scope.row.shopContractStatus == 2" type="error"
              >已否决</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <div class="operation">
              <div
                class="list"
                @click.prevent="approveHit(scope.row)"
                v-if="scope.row.shopContractStatus == 0"
              >
                去审批
              </div>
              <div class="list" @click.prevent="downloadHit(scope.row)">
                下载
              </div>
              <div class="list" @click.prevent="uploadingHit(1, scope.row)">
                上传
              </div>
              <div class="list" @click.prevent="detailsHit(scope.row)">
                详情
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="tableDataTotal"
          :current-page="page"
        >
        </el-pagination>
      </div>
    </div>

    <!--新增或者编辑-->
    <div class="add">
      <el-dialog
        title="审核页面"
        :visible.sync="addVisible"
        :before-close="addClose"
        width="30%"
      >
        <div class="add-form">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
          >
            <el-form-item label="申请人">
              <el-input
                v-model="ruleForm.name"
                placeholder="请输入申请人"
                clearable
                readonly
              ></el-input>
            </el-form-item>

            <el-form-item label="合同审批备注" prop="text">
              <el-input
                type="textarea"
                :rows="4"
                v-model="ruleForm.text"
                placeholder="请输入合同审批备注"
              ></el-input>
            </el-form-item>

            <el-form-item class="attachment">
              <div class="list" @click="addRefuse">拒绝</div>
              <div class="list" @click="addConfirm('ruleForm')">通过</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>

    <!--上传-->
    <div class="uploading">
      <el-dialog
        title="上传"
        :visible.sync="uploadingShow"
        :before-close="uploadingClose"
        width="30%"
      >
        <div class="add-form">
          <el-form
            :model="uploadingruleForm"
            :rules="uploadingrules"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
          >
            <el-form-item label="合同pdf">
              <el-upload
                action="https://api-qingchuang.xmw1688.com/common/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
              >
                <div class="img">
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                </div>
              </el-upload>
              <a :href="uploadingruleForm.icon">{{ uploadingruleForm.icon }}</a>
            </el-form-item>

            <el-form-item class="attachment">
              <div class="list" @click="uploadingClose">取消</div>
              <div class="list" @click="uploadingConfirm('ruleForm')">通过</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  contractShopList,
  contractShopListId,
  contractShopListAudit,
} from "@/api/contract";

export default {
  data() {
    return {
      commonValue: "",
      commonUpdateOptions: [
        {
          value: "0",
          label: "待审批",
        },
        {
          value: "1",
          label: "已通过",
        },
        {
          value: "2",
          label: "已否决",
        },
      ],
      commonUpdateValue: "",
      commonDate: [],
      commonStart: "",
      commonEnd: "",
      //-------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      loading: false,
      commonObj: {},
      multipleSelection: [],
      //-------------
      addVisible: false,
      addRoaid: 0,
      ruleForm: {
        text: "",
      },
      rules: {
        text: [
          { required: true, message: "请输入合同审批备注", trigger: "blur" },
        ],
      },
      //--------
      uploadingruleForm: {
        icon: "",
      },
      uploadingrules: {
        icon: [{ required: true, message: "请上传合同pdf", trigger: "blur" }],
      },
      uploadingShow: false,
    };
  },
  created() {
    this.contractShopListMet();
  },
  methods: {
    contractShopListMet() {
      let params = {
        keyWord: this.commonValue,
        startTime: this.commonStart,
        endTime: this.commonEnd,
        shopContractStatus: this.commonUpdateValue,

        pageSize: this.limit,
        pageNo: this.page,
      };

      contractShopList(params).then((res) => {
        if (res.errorCode == null) {
          this.tableData = res.resultData.contractList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      });
    },
    //-------
    searchHit() {
      if (this.commonDate.length > 0) {
        this.commonStart = this.timeData(new Date(this.commonDate[0]));
        this.commonEnd = this.timeData(new Date(this.commonDate[1]));
      } else {
        this.commonStart = "";
        this.commonEnd = "";
      }

      this.loading = true;
      this.contractShopListMet();
    },
    replacementHit() {
      this.commonValue = "";
      this.commonDate = [];
      this.commonStart = "";
      this.commonEnd = "";
      this.commonUpdateValue = "";

      this.loading = true;
      this.contractShopListMet();
    },
    //-------------
    approveHit(item) {
      this.commonObj = item;
      this.addVisible = true;
    },
    downloadHit(item) {
      let params = item.shopId;

      contractShopListId(params).then((res) => {
        if (res.errorCode == null) {
          window.open(item.shopContractUrl, "_blank");
        }
      });
    },
    uploadingHit(index, item) {
      this.addRoaid = index;

      if (index == 1) {
        this.commonObj = item;
        let params = item.shopId;

        contractShopListId(params).then((res) => {
          if (res.errorCode == null) {
            this.uploadingruleForm.icon = item.shopContractUrl;
            this.uploadingShow = true;
          }
        });
      } else {
        this.uploadingruleForm.icon = "";
        this.uploadingShow = true;
      }
    },
    uploadingClose() {
      this.uploadingShow = false;
    },
    uploadingConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
        } else {
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.uploadingruleForm.icon = res.resultData;
    },
    //-----------
    detailsHit(item) {
      this.$router.push({
        path: "/contract/list-details",
        query: {
          id: item.shopId,
        },
      });
    },
    //-----
    addClose() {
      this.addVisible = false;
    },
    addRefuse() {
      this.contractShopListAuditMer(2);
    },
    addConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.contractShopListAuditMer(1);
        } else {
          return false;
        }
      });
    },

    contractShopListAuditMer(index) {
      let params = {
        shopId: this.commonObj.shopId,
        shopContractStatus: index,
        shopContractBreak: this.ruleForm.text,
      };

      contractShopListAudit(params).thne((res) => {
        if (res.errorCode == null) {
          this.$message.success("审核成功！");
          this.addVisible = false;
          this.contractShopListMet();
        }
      });
    },

    //----------------------
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //---------
    timeData(date) {
      return date.toLocaleString("zh-CN", {
        year: "numeric",
        month: "2-digit",
        day: "2-digit",
        // hour: "2-digit",
        // minute: "2-digit",
        // second: "2-digit",
      });
    },
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.contractShopListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.contractShopListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/common.less");

.contract {
  .add,
  .uploading {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 70px;
        height: 70px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;
        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>